<template>
  <Row class="data-dictionary ">
    <Col span="24" class="col-main">
      <Form label-position="top">
        <Row v-if="!(personType === 3 && formData.isDestroy === '0')"><!--项目部 待销号 搜索不可操作-->
          <Col span="20">
            <Row>
              <Col span="5" v-if="this.personType === 1">
                <FormItem label style="margin-bottom: 14px;">
                  <Select
                    v-model="formData.enterpriseId"
                    style="width: 90%;"
                    ref="treeSelect"
                    placeholder="请选择机构"
                  >
                    <Option
                      v-for="item in list1"
                      :value="item.enterpriseId"
                      :key="item.enterpriseId"
                      style="display: none;"
                    >{{ item.title }}</Option
                    >
                    <Tree
                      :data="orgList"
                      ref="tree"
                      style="padding:0 10px"
                      @on-select-change="checkEng"
                    ></Tree>
                  </Select>
                </FormItem>
              </Col>
              <Col span="5">
                <FormItem label style="margin-bottom: 14px;">
                  <Select
                    v-model="formData.projectTeamId"
                    :clearable="proList.length>0 && personType !=3"
                    style="width: 90%;"
                    placeholder="请选择项目"
                  >
                    <Option
                      v-for="item in proList"
                      :value="item.id"
                      :key="item.id"
                    >{{ item.shortName }}</Option
                    >
                  </Select>
                </FormItem>
              </Col>
              <Col span="5">
                <FormItem label style="margin-bottom: 14px;">
                 <!-- <Select
                    v-model="formData.yearMonth"
                    style="width: 90%;"
                    placeholder="请选择时间">
                    <Option
                      v-for="item in yearList"
                      :value="item"
                      :key="item"
                    >{{ item}}</Option>
                  </Select>-->
                  <DatePicker type="month" :clearable="false" :editable="false"  @on-change="dateChange" placeholder="请选择时间" :value="formData.yearMonth" style="width: 90%"></DatePicker>
                </FormItem>
              </Col>
              <Col span="5">
                <FormItem label style="margin-bottom: 14px;">
                  <Select
                    v-model="formData.status"
                    clearable
                    style="width: 90%;"
                    placeholder="状态">
                    <Option
                      v-for="item in statusList"
                      :value="item.id"
                      :key="item.id"
                    >{{ item.name }}</Option>
                  </Select>
                </FormItem>
              </Col>
            </Row>
          </Col>
          <Col span="4" style="text-align: right;">
            <div class="button-s">
              <Button
                type="primary"
                style="margin-right: 15px"
                @click="page.current =1,searchBtn()"
                v-if="btnShow.search"
              >查询</Button
              >
              <!--<Button type="warning" @click="resetBtn">重置</Button>-->
            </div>
          </Col>
        </Row>
      </Form>
      <div class="button-s">
        <Button
          type="primary"
          v-if="btnShow.add && personType===3 && isDispatch"
          @click="addBtn('add', '1', '下月动态新增')">新增</Button>
        <Button
          type="primary"
          v-if="btnShow.dispose && isDispatch"
          @click="addBtn('dispose', '1', '每月动态配置')">配置</Button>
        <!--<Button type="primary" @click="deleteBtn">删除</Button>-->
      </div>
      <Table
        border
        ref="selection"
        :columns="columns"
        :data="dictList"
        @on-select="selectRiado"
        @on-select-all="selectRiadoAll"
        @on-select-cancel="selectCancel"
        @on-select-all-cancel="selectAllCancel"
      >
        <template slot-scope="{ row, index }" slot="status">
          <span
            v-if="row.status==='39'
            || row.status==='69'
            || row.status==='89'"
            style="color: #3BBB0A">{{row.statusName}}</span> <!--通过-->
          <span
            v-if="row.status==='34'
            || row.status==='64'
            || row.status==='84'" style="color: #EE4044">{{row.statusName}}</span><!--驳回-->
          <span
            v-if="row.status==='12'
            || row.status==='23'
            || row.status==='42'
            || row.status==='105'
            || row.status==='56'
            || row.status==='117'
            || row.status==='78'
            " style="color: #E7B167">{{row.statusName}}</span> <!--待审-->
          <span v-if="row.status==='310' || row.status==='611'" style="color: #3170C8">{{row.statusName}}</span> <!--审核-->
        </template>
        <template slot-scope="{ row, index }" slot="action">
          <!--集团/公司-->
          <span
            title="接受"
            class="button-class"
            type="text"
            v-if="btnShow.accept && row.buttonReceive"
            @click="addBtn('accept',row, '每月动态接收')" >接收</span>
          <!--项目-->
          <span
            title="编辑"
            class="button-class"
            type="text"
            v-if="btnShow.edit && row.buttonEdit"
            @click="addBtn('edit', row.dynamicRiskOrderListId, '每月动态编辑')" >编辑</span>
            <!-- v-if="btnShow.edit && row.buttonEdit" -->
          <span
            title="修订"
            class="button-class"
            type="text"
            v-if="btnShow.revise && row.buttonRevise"
            @click="addBtn('revise', row.dynamicRiskOrderListId, '每月动态修订')" >修订</span>
          <span
            title="审核"
            class="button-class"
            type="text"
            v-if="btnShow.examine && row.buttonApproval"
            @click="addBtn('examine', row, '每月动态审核',1)">审核</span>
          <span
            title="签发"
            class="button-class"
            type="text"
            v-if="btnShow.issue && row.buttonIssue"
            @click="addBtn('issue', row, '每月动态签发',2)">签发</span>
          <span
            title="详情"
            class="button-class"
            type="text"
            v-if="btnShow.detail"
            @click="addBtn('details', row.dynamicRiskOrderListId, '每月动态详情')">详情</span>
          <span
            title="删除"
            class="button-class"
            type="text"
            v-if="btnShow.del && row.buttonDelete"
            @click="deleteBtn(row)" >删除</span>
        </template>
      </Table>
      <Page
        class="pagination"
        :page-size-opts="[10,20,50,100]"
        :total="page.total"
        :current="page.current"
        :page-size="page.pageSize"
        @on-change="pageChange"
        @on-page-size-change="sizeChange"
        show-elevator
        show-sizer
        show-total
      />
    </Col>
  </Row>
</template>

<script>
  export default {
    name: "monthly-dynamics",
    data(){
      return{
        btnShow:{},
        rowData:'',
        isDispatch: false,
        userDetail:JSON.parse(localStorage.getItem('userList')),
        personType:JSON.parse(localStorage.getItem('personType')),
        yearList:[],
        formData:{
          enterpriseId:'',
          projectTeamId:'',
          /*pointName:'',
          riskLevel:'',*/
          yearMonth:this.initDate(),
          status:''
        },
        scheduleData:'',
        orgList: [], //机构树
        proList: [], //项目部树
        dictList:[],
        list1:[],
        list2:[],
        projectList:[],
        treeList:[],
        riskLevelList: [
          { id: '1', name: 'Ⅰ级' },
          { id: '2', name: 'Ⅱ级' },
          { id: '3', name: 'Ⅲ级' },
          { id: '4', name: 'Ⅳ级' }
        ],
        statusList: [
          { id: 'PROJECT_READY', name: '项目待审' },
          { id: 'PROJECT_ALREADY', name: '项目已审' },
          { id: 'ENTERPRISE_READY', name: '公司待审' },
          { id: 'ENTERPRISE_ALREADY', name: '公司已审' },
          { id: 'GROUP_READY', name: '集团待审' },
          { id: 'PASS', name: '已通过' },
          { id: 'REJECT', name: '审核驳回' },
        ],
        columns:[
          {
            title: '序号',
            align: 'center',
            render: (h, params) => {
              return h(
                'span',
                params.index + (this.page.current - 1) * this.page.pageSize + 1
              )
            }
          },
          {
            align: 'center',
            title:'公司名称',
            key:'orgName',
            tooltip:true,
          },
          {
            align: 'center',
            title:'项目名称',
            key:'proName',
            tooltip:true,
          },
          {
            title: '动态管控表名称',
            key: 'dynamicRiskOrderListName',
            align: 'center',
            tooltip:true,
          },{
            title: '作业风险总数',
            key: 'riskAllCnt',
            align: 'center',
          },{
            title: 'Ⅰ级风险',
            key:'riskOneCnt',
            align: 'center',
          },{
            title: 'Ⅱ级风险',
            key:'riskTwoCnt',
            align: 'center',
          },{
            title: 'Ⅲ级风险',
            key:'riskThreeCnt',
            align: 'center',
          },{
            title: 'Ⅳ级风险',
            key:'riskFourCnt',
            align: 'center',
          },{
            title: '管控措施总数',
            key:'measuresCnt',
            align: 'center',
          },{
            title: '状态',
            align: 'center',
            slot:'status'
          },
          {
            title: '操作',
            width: 200,
            align: 'center',
            slot: 'action'
          }
        ],
        deleteList:[],
        proId:[],
        proIdCopy:[],
        page: {
          total: 0,
          current: 1,
          pageSize: 10,
          totalPage: 0
        },
      }
    },
    methods:{
      //查询当前用户是否为调度员
      checkPerson(){
        this.$api.measurePlan.checkDispatch().then(res => {
          if(res.code === 200){
            this.isDispatch = true;
          } else {
            this.isDispatch = false;
          }
        })
      },
      //列表
      getWbsTabelList(){
        let param ={
          currentPage: this.page.current,
          pageSize: this.page.pageSize,
          whereMap: {
            "proId": this.personType === 3?[this.formData.projectTeamId]:this.proId,//"项目id",
            "yearMonth": this.formData.yearMonth,//"年月",
            "status":this.formData.status
          }
        };
        this.$api.MonthlyDynamics.getDynamicRiskOrderList(param).then(res => {
          if(res.code === 200){
            this.dictList = res.data.resultObject;
            this.page.total = res.data.totalRecord;
            this.page.totalPage = res.data.totalPage
          }else {
            this.$Message.warning(res.message)
          }
        })
      },
      //机构树
      getOrgTree() {
        this.orgList = [];
        this.list1 = [];
        this.$api.measurePlan.getOrgTree().then((res) => {
          if (res.code == 200) {
            this.orgList = res.data
            const tree = this.$refs.tree
            this.$nextTick(() =>  tree.handleSelect(tree.data[0].nodeKey))
          } else {
            this.$Message.error(res.data.msg);
          }
        })
      },
      checkEng(data) {//点击机构树
        if (data.length > 0) {
          this.formData.projectTeamId = '';
          this.list1 = []
          let title = data[0].title
          if(data[0].objectId){
            let enterpriseId = data[0].objectId
            this.list1.push({
              title,
              enterpriseId
            })
            this.getProTree(data[0]);
            this.formData.enterpriseId = enterpriseId
            this.$refs.treeSelect.visible = false
          }else {
            this.$Message.warning('请选择'+title+'下的子集')
          }
        }
      },
      //获取项目列表
      getProTree(data) {
        this.proList = [];
        this.$api.measurePlan.getProList(data.objectId).then(res => {
          if (res.code === 200) {
            if(res.data.length>0){
              this.proList = res.data;
              this.proId = [];
              this.proIdCopy = [];
              if(this.proList.length>0){
                this.proList.forEach(item => {
                  this.proId.push(item.id);
                  this.proIdCopy.push(item.id);
                })
              }
              if(this.personType === 3){
                this.formData.projectTeamId = this.proList[0].id;
              }else{
                this.formData.projectTeamId = '';
              }
              this.getWbsTabelList();
            }
          } else {
            this.$Message.error(res.message)
          }
        })
      },
      /*checkPro(data) {//点击项目树
        if (data.length > 0) {
          this.list2 = []
          let proId = data[0].id
          let title = data[0].name
          this.list2.push({
            proId,
            title
          })
          this.formData.projectTeamId = proId
          this.$refs.treeSelectPro.visible = false
        }
      },*/
      addBtn(type, id, name,num) {
        if (type === 'add') {
          let objectId = ''
          if(this.personType == 1 && !this.formData.enterpriseId){
            this.$Message.warning('请先选择机构')
            return
          }else{
            objectId = this.formData.enterpriseId
          }
          if(this.personType == 2){
            objectId = this.userDetail.enterpriseId
          }
          if(this.personType == 3){
            objectId = this.userDetail.projectTeamId
          }
          this.$router.push({
            name:'dynamics-add',
            params:{
              type:type,
              id:objectId,
              name:name
            },
            query:{
              proId:this.formData.projectTeamId,
            }
          })
        }else if(type === 'examine'){//审核
          this.$router.push({
            name: 'monthly-examine',
            params:{type,id:id.dynamicRiskOrderListId,name},
            query:{isIssue:num,proName:id.proName,dynamicRiskOrderListName:id.dynamicRiskOrderListName}
          })
        }else if(type === 'issue'){//签发
          this.$router.push({
            name: 'monthly-issue',
            params:{type,id:id.dynamicRiskOrderListId,name},
            query:{isIssue:num,proName:id.proName,dynamicRiskOrderListName:id.dynamicRiskOrderListName}
          })
        } else if(type === 'accept'){//接受
          this.$router.push({
            name: 'monthly-accept',
            params:{type,id:id.dynamicRiskOrderListId,name},
            query:{proName:id.proName,dynamicRiskOrderListName:id.dynamicRiskOrderListName}
          })
        }else if(type === 'revise'){//修订
          this.$router.push({
            name: 'monthly-revise',
            params:{type,id,name}
          })
        }else if(type === 'dispose'){//配置
          this.$router.push({
            name: 'monthly-dynamics-dispose',
            params:{type,id,name}
          })
        }else if(type === 'details'){//详情
          this.$router.push({
            name: 'monthly-dynamics-detail',
            params:{type,id,name}
          })
        }else if(type == 'edit'){
          this.$router.push({
            name:'dynamics-edit',
            params:{
              id:id
            }
          })
        }
      },
      deleteBtn(row){
        this.$Modal.confirm({
          title: '温馨提示',
          closable: true,
          content: '确认删除吗？',
          width: 360,
          onOk: () => {
            this.$api.MonthlyDynamics
              .getDynamicRiskOrderremove(row.dynamicRiskOrderListId)
              .then(res => {
                if (res.code === 200) {
                  this.$Message.success(res.message)
                  this.getWbsTabelList()
                } else {
                  this.$Message.error(res.message)
                }
              })
          }
        })
      },
      tabpaneBtn(e){//tab切换
        this.page.current =1;
        this.page.currentPage =10;
        this.formData.isDestroy = e;
        this.getWbsTabelList();
      },
      //搜索
      searchBtn() {
        if (this.personType !== 3) {
          this.proId = [this.formData.projectTeamId];
          if(!this.formData.projectTeamId){
            this.proId = this.proIdCopy;
          }
        }
        this.getWbsTabelList()
      },
      //重置
      resetBtn() {
        this.formData.projectTeamId = '';
        this.formData.status = '';
        this.formData.pointName = '';
        this.formData.riskLevel = '';
        this.formData.destroyStage = '';
        this.formData.riskCategoryId = '';
        this.getWbsTabelList()
      },
     /* yearfc(){
        let year = new Date().getFullYear(); //得到年份
        let month = new Date().getMonth();//得到月份
        let yearList = [];
        for (let i =2018;i<=year;i++){
          if(i == year){
            for (let j =1;j<=month+1;j++){
              if(j<=9){
                j='0'+j
              }
              let b ='';
              b = i +'-' +j;
              yearList.unshift(b)
            }
          }else {
            for (let j =1;j<=12;j++){
              if(j<=9){
                j='0'+j
              }
              let b ='';
              b = i+ '-' +j;
              yearList.unshift(b)
            }
          }
        }
        this.yearList = yearList;
        this.formData.yearMonth = this.yearList[0];
      },*/
      initDate() {
        const date = new Date()
        const year = date.getFullYear()
        let month = date.getMonth() + 1
        month = month > 9 ? month : `0${month}`
        return `${year}-${month}`
      },
      dateChange(date){
        this.formData.yearMonth = date;
      },
      //全选
      selectRiadoAll(e) {
        this.deleteList = e
      },
      //全选取消
      selectAllCancel() {
        this.deleteList = []
      },
      //单选
      selectRiado(e) {
        this.deleteList = e
      },
      //取消某一项
      selectCancel(e) {
        this.deleteList.splice(e, 1)
      },
      pageChange(current) {
        this.page.current = current
        this.getWbsTabelList(this.parentId)
      },
      sizeChange(size) {
        this.page.current = 1
        this.page.pageSize = size
        this.getWbsTabelList(this.parentId)
      },
      //按钮显示
      buttonShow() {
        let buttonList = JSON.parse(localStorage.getItem('buttonList'))
        buttonList.map(val => {
          let key = Object.keys(val).toString();
          if (key === this.$route.name) {
            Object.values(val)[0].map(item => {
              if (item.code === 'P_1001_01') {
                this.btnShow.add = true
              } else if (item.code === 'P_1001_02') {
                this.btnShow.dispose = true
              } else if (item.code === 'P_1001_03') {
                this.btnShow.search = true
              }else if (item.code === 'P_1001_04') {
                this.btnShow.edit = true
              }else if (item.code === 'P_1001_05') {
                this.btnShow.detail = true
              }else if (item.code === 'P_1001_06') {
                this.btnShow.del = true
              }else if (item.code === 'P_1001_07') {
                this.btnShow.examine = true
              }else if (item.code === 'P_1001_08') {
                this.btnShow.issue = true
              }else if (item.code === 'P_1001_09') {
                this.btnShow.revise = true
              }else if (item.code === 'P_1001_10') {
                this.btnShow.accept = true
              }
            })
          }
        })
      },
    },
    mounted() {
      this.buttonShow();
      this.checkPerson();
      //1集团 2工程公司 3项目部
      if (this.personType === 3) {
        this.columns.splice(1,2);
        this.getProTree({objectId:this.userDetail.projectTeamId})
      }else if(this.personType === 1){
        this.getOrgTree();
      }else if(this.personType === 2){
        this.columns.splice(1,1);
        this.getProTree({objectId:this.userDetail.enterpriseId})
      }
    },
    activated() {
      if (this.personType === 3) {
        this.getProTree({objectId:this.userDetail.projectTeamId})
      }else if(this.personType === 1){
        this.getOrgTree();
      }else if(this.personType === 2){
        this.getProTree({objectId:this.userDetail.enterpriseId})
      }
    }
  };
</script>

<style lang="scss" scoped>
  .data-dictionary {
    height: calc(100% - 0px);
    .col-height100 {
      height: 100%;
      border-right: 1px solid #ebeef1;
      overflow: auto;
      p {
        padding: 10px;
      }
    }
    .col-main {
      padding: 10px;
      .button-s {
        margin-bottom: 15px;
        button {
          margin-right: 15px;
        }
      }
    }
    .tabs_box {
      color: #f00 !important;
    }
  }
</style>
